#include "CBufferDC.h"


CBufferDC::CBufferDC(HWND hWnd, UINT nWidth, UINT nHeight):
m_nWidth(nWidth),
m_nHeight(nHeight),
m_hWndDC(NULL)
{
	m_hWndDC = ::GetDC(hWnd);
}

void CBufferDC::BeginRender()
{
	m_hBufferDC = CreateCompatibleDC(NULL);
	m_hBitmap = CreateCompatibleBitmap(m_hWndDC, m_nWidth, m_nHeight);
	m_hOldBitmap = (HBITMAP)SelectObject(m_hBufferDC,  m_hBitmap);
}

void CBufferDC::BitBlt(int nDesX, int nDesY, int nDesWidth, int nDesHeight, HDC hSrcDC, int nSrcX, int nSrcY)
{
	::BitBlt(m_hBufferDC, nDesX, nDesY, nDesWidth, nDesHeight, hSrcDC, nSrcX, nSrcY, SRCCOPY);
}

void CBufferDC::StretchBlt(int nDesX, int nDesY, int nDesWidth, int nDesHeight, HDC hSrcDC,
					int nSrcX, int nSrcY, int nSrcWidth, int nSrcHeight)
{
	::StretchBlt(m_hBufferDC, nDesX, nDesY, nDesWidth, nDesHeight, hSrcDC, nSrcX, nSrcY, nSrcWidth, nSrcHeight, SRCCOPY);
}

void CBufferDC::TransparentBlt(int nDesX, int nDesY, int nDesWidth, int nDesHeight, HDC hSrcDC,
					  int nSrcX, int nSrcY, int nSrcWidth, int nSrcHeight, COLORREF colorRef)
{
	::TransparentBlt(m_hBufferDC, nDesX, nDesY, nDesWidth, nDesHeight, hSrcDC, nSrcX, nSrcY, nSrcWidth, nSrcHeight, colorRef);
}

void CBufferDC::EndRender()
{
	::BitBlt(m_hWndDC, 0, 0, m_nWidth, m_nHeight, m_hBufferDC, 0, 0, SRCCOPY);
	SelectObject(m_hBufferDC, m_hOldBitmap);

	DeleteObject(m_hBitmap);
	DeleteDC(m_hBufferDC);
}